/*
 *     BasicDao.java  Copyright (c) 2021. cc01cc. All rights reserved.
 *
 *     This file is part of e-commerce.
 *
 *     e-commerce is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     e-commerce is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* 基础数据库操作
* */
package dao;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BasicDao {
    private static String JDBC_DRIVER;
    private static String url;
    private static String username;
    private static String password;

    static{
        Properties properties = new Properties();
        InputStream is = BasicDao.class.getClassLoader().getResourceAsStream("db.properties");
        try{
            properties.load(is);
        }catch(IOException e){
            e.printStackTrace();
        }
        JDBC_DRIVER = properties.getProperty("JDBC_DRIVER");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
//        url = "jdbc:mysql://47.106.238.147:3306?useUnicode=true&characterEncoding=utf-8";
//        username = "TEST01";
//        password = "Mysql-01";
    }

    public static Connection getConnection(){
        Connection connection = null;

        try{
//            Tomcat 中需要手动加载驱动
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(url, username, password);
        }catch(Exception e){
            e.printStackTrace();
        }
        return connection;
    }
    @Test
    public void testGetConnection(){
        Connection connection = null;
        try{
            connection = DriverManager.getConnection(url, username, password);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static ResultSet execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement, ResultSet resultSet)throws SQLException{
        preparedStatement = connection.prepareStatement(sql);

        for(int i = 0; i < params.length; i++){
            System.out.println("BasicDao: " + params[i]);
            preparedStatement.setObject(i+1, params[i]);
        }


        resultSet = preparedStatement.executeQuery();
//        while(resultSet.next()){
//            String user_info = resultSet.getObject("user_name").toString();
//            System.out.println("用户名称"+resultSet.getObject("user_name"));
//        }
        return resultSet;
    }

    public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement)throws SQLException{
        preparedStatement = connection.prepareStatement(sql);

        for(int i = 0; i < params.length; i++){
            preparedStatement.setObject(i+1, params[i]);
        }

        int updateRows = preparedStatement.executeUpdate();

        return updateRows;
    }

    public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
        boolean resourceClosed = true;
        if(resultSet != null){
            try{
                resultSet.close();
                resultSet = null;
            }catch(SQLException e){
                e.printStackTrace();
                resourceClosed = false;
            }
        }
        if(preparedStatement != null){
            try{
                preparedStatement.close();
                preparedStatement = null;
            }catch(SQLException e){
                e.printStackTrace();
                resourceClosed = false;
            }
        }
        if(connection != null){
            try{
                connection.close();
                connection = null;
            }catch(SQLException e){
                e.printStackTrace();
                resourceClosed = false;
            }
        }
        return resourceClosed;
    }
}
